﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SalePartial.Utility;
using SalePartial.BLL;
using SalePartial.Model;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        //'获取当前用户名
        //Dim selectedUserName As String = Page.User.Identity.Name

        //'获取当前用户ID
        //Dim member As MembershipUser = Membership.GetUser(selectedUserName)

        //Dim CurrentUserID As Guid = member.ProviderUserKey

        //'获取用户所属角色
        //Dim selectedUsersRoles As String() = Roles.GetRolesForUser(selectedUserName)


        if ((Page.User.Identity.IsAuthenticated))
        {
            hlkReg.Visible = false;
            hlkAccount.Visible = true;

        }
        else
        {
            hlkReg.Visible = true;
            hlkAccount.Visible = false;
        }
    }


    protected void hlkAccount_Click(object sender, System.EventArgs e)
    {
        //string urlTemp = "~/user/account/profile.aspx";
        string urlTemp = "~/Login/regConfirm.aspx";

        string username = Page.User.Identity.Name;

        if (!username.Equals(""))
        {
            Session.Remove(ConstantSession.REG_PERPON_USER_INFO);
            Session.Remove(ConstantSession.REG_PERPON_USER_PROFILE);

            Session.Remove(ConstantSession.REG_PURCH_COMPY_INFO);
            Session.Remove(ConstantSession.REG_PURCH_COMPYCERT_INFO);
            Session.Remove(ConstantSession.REG_PURCH_COMPYCERT_LIST);

            Session.Remove(ConstantSession.REG_SUPPLY_COMPY_INFO);
            Session.Remove(ConstantSession.REG_SUPPLY_COMPYCERT_INFO);
            Session.Remove(ConstantSession.REG_SUPPLY_COMPYCERT_LIST);
            Session.Remove(ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST);

            MembershipUser reguser = Membership.GetUser(username);

            RegUserInfo userinfo = new RegUserInfo();
            userinfo.Username = reguser.Email;
            //userinfo.Password = reguser.GetPassword();
            userinfo.PasswordQuestion = reguser.PasswordQuestion;
            userinfo.Email = reguser.Email;
            userinfo.IsApproved = reguser.IsApproved;
            userinfo.PasswordQuestion = reguser.PasswordQuestion;
            userinfo.ProviderUserKey = reguser.ProviderUserKey;
            userinfo.Username = reguser.UserName;
            //userinfo.PasswordAnswer = reguser.

            Guid userid = new Guid(reguser.ProviderUserKey.ToString());
            XPUserProfile xPUPF = new XPUserProfile();
            XPUserProfileInfo profileInfo = xPUPF.SelectInfoByUserID(userid);

            Session[ConstantSession.REG_PERPON_USER_INFO] = userinfo;
            Session[ConstantSession.REG_PERPON_USER_PROFILE] = profileInfo;

            XPCompany bilCompany = new XPCompany();
            XPCompanyCert bilCorpcert = new XPCompanyCert();
            XPCompanyProduct bilXPCP = new XPCompanyProduct();

            List<XPCompanyInfo> companyInfoList = bilCompany.SelectInfoByUserID(userid);
            foreach(XPCompanyInfo info in companyInfoList){
                if (info.IsApproved == true)
                {
                    Session[ConstantSession.REG_PURCH_COMPY_INFO] = info;

                    List<XPCompanyCertInfo> certItems = bilCorpcert.SelectInfoByCompanyID(info.CompanyID);
                    foreach (XPCompanyCertInfo item in certItems)
                    {
                        if (item.CertType == 1)
                        {
                            Session[ConstantSession.REG_PURCH_COMPYCERT_INFO] = item;
                        }
                        else if (item.CertType == 2)
                        {
                            if(Session[ConstantSession.REG_PURCH_COMPYCERT_LIST] == null)
                            {
                                Session[ConstantSession.REG_PURCH_COMPYCERT_LIST] = new List<XPCompanyCertInfo> ();
                            }
                            (( List<XPCompanyCertInfo>)Session[ConstantSession.REG_PURCH_COMPYCERT_LIST]).Add(item);
                        }
                    }

                }
                else if (info.IsApproved == false)
                {
                    Session[ConstantSession.REG_SUPPLY_COMPY_INFO] = info;

                    List<XPCompanyCertInfo> certItems = bilCorpcert.SelectInfoByCompanyID(info.CompanyID);
                    foreach (XPCompanyCertInfo item in certItems)
                    {
                        if (item.CertType == 1)
                        {
                            Session[ConstantSession.REG_SUPPLY_COMPYCERT_INFO] = item;
                        }
                        else if (item.CertType == 2)
                        {
                            if (Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST] == null)
                            {
                                Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST] = new List<XPCompanyCertInfo>();
                            }
                            ((List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST]).Add(item);
                        }
                    }

                    List<XPCompanyProductInfo> productInfo = bilXPCP.SelectInfoByCompanyID(info.CompanyID);
                    Session[ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST] = productInfo;
                }
            }

            Response.Redirect(urlTemp, true);

        }
        else
        {
            Response.Redirect("~/error.aspx?ErrCode=3&url=" + urlTemp, true);
        }

    }


    protected void hlkLogin_LoggingOut(object sender, System.Web.UI.WebControls.LoginCancelEventArgs e)
    {
        MembershipUser member = Membership.GetUser(Page.User.Identity.Name);
        if (member == null)
        {
            Response.Redirect("~/error.aspx?ErrCode=3", true);
        }
    }
    public _Default()
    {
        Load += Page_Load;
    }

    protected void hlkLogin_LoggedOut(object sender, EventArgs e)
    {
        Session.Clear();
        Session.Abandon();
        FormsAuthentication.SignOut();

    }
}